package com.sdy.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sdy.dao.redis.CompanyDao;
import com.sdy.dao.mysql.SerialMapper;
import com.sdy.po.Parm;
import com.sdy.po.SerialPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService{

    @Autowired
    NoticService noticService;
    @Resource
    CompanyDao companyDao;
    @Resource
    SerialMapper serialMapper;

//    /**
//     * 获取客户公司识别码
//     * @param companyId
//     * @return
//     */
//    public String getConpanyCode(Integer companyId){
//        return companyDao.getConpanyCode(companyId);
//    }

    /**
     * 判断CompanyCode是否存在
     * @return
     */
    public Boolean isCodeExist(String code){
        return companyDao.isKeyExist("companyCode:"+code);
    }

    /**
     * 根据识别码获得公司id，获得串口信息
     * @param code
     * @return
     */
    public String getSerialInfo(String code){
        JSONObject jsonObject = new JSONObject();
        if (isCodeExist(code)){
            // 先通过识别码向redis查询到客户公司的id
            // 再用id去Mysql查询该公司注册的串口信息
            List<SerialPO> serialList = serialMapper.getSerialListByCompanyId(companyDao.getCompanyId(code));
            jsonObject.put("result",true);
            jsonObject.put("company",2);
            jsonObject.put("data", JSON.toJSONString(serialList));
            return jsonObject.toJSONString();
        }
        jsonObject.put("result",false);
        return jsonObject.toJSONString();
    }

    /**
     * 判断该公司的数据是否存在
     * @param companyId
     * @param parmName
     * @return
     */
    @Override
    public Boolean isParmStateExit(Integer companyId, String parmName) {
        return companyDao.isKeyExist("parmState:"+companyId+":"+parmName);
    }

    /**
     * 检查redis中的运行状态和最新的是否一致
     * 如果不一致，要改变状态，要发推送
     * @param companyId
     * @param parm
     * @param data
     */
    @Override
    public void setParmState(Integer companyId, Parm parm, JSONObject data) {
        String nowState = data.get("dataState").toString();
        if (isParmStateExit(companyId,parm.geteName())){
            String beforeState = companyDao.getParmState(companyId,parm.geteName());
            if (beforeState.equals(nowState)){
                return;
            }
            System.out.println("不一样！");
            noticService.insertNotic(beforeState,companyId,parm,data);
            companyDao.setParmState(companyId,parm,nowState);
            return;
        }
        companyDao.setParmState(companyId,parm,nowState);
    }

    /**
     * 重新设定过期时间
     * @return
     */
    public Boolean updateExpirationTime(String code, Integer companyId, Integer time){
        if (companyDao.setConnectCode(companyId,code,time) && companyDao.setConpanyCode(companyId,code,time)){
            System.out.println("redis成功更新");
            return true;
        }
        return false;
    }
}
